package com.open.capacity.study.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysTeaching;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.study.dao.TeachingSystemDao;
import com.open.capacity.study.service.TeachingSystemService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class TeachingSystemServiceImpl implements TeachingSystemService {
    @Autowired
    private TeachingSystemDao teachingSystemDao;

    /**
     * 增加
     * @param sysTeaching
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional
    public Result insert(SysTeaching sysTeaching) throws ServiceException {
        try {
           if(StringUtil.isBlank(sysTeaching.getTtitle())){
                return Result.failed("体系名不能为空！");
           }
           int res = 0;
           SysTeaching addTeaching = teachingSystemDao.findByTitle(sysTeaching.getTtitle());
           //根据体系名判断体系是否存在
            if (addTeaching != null) {
                if (addTeaching.getTtitle() != null) {
                    return Result.failed("该体系名已存在，请重新输入");
                }
            }
            sysTeaching.setStatus(1);
            sysTeaching.setCretim(new Date());
           res = teachingSystemDao.insert(sysTeaching);

           log.info("添加教学体系：{}",sysTeaching);
           return res>0 ? Result.succeed(sysTeaching,"添加成功") : Result.failed("添加失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    /**
     * 修改可用状态
     * @param params
     * @return
     * @throws ServiceException
     */
    @Override
    public Result updateStatus(Map<String, Object> params) throws ServiceException {
        try {
            Integer id = MapUtils.getInteger(params,"tcstid");
            int status = MapUtils.getIntValue(params,"status");
            SysTeaching updateTeaching = teachingSystemDao.findById(id);
            if (updateTeaching == null){
                return Result.failed("不存在该体系");
            }
            updateTeaching.setStatus(status);
            updateTeaching.setUpdtim(new Date());
            int res = teachingSystemDao.updateStatusById(updateTeaching);
            log.info("修改教学体系：{}",updateTeaching);
            return res>0 ? Result.succeed(updateTeaching,"修改成功") : Result.failed("修改失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    /**
     * 修改体系信息
     * @param sysTeaching
     * @return
     * @throws ServiceException
     */
    @Override
    @Transactional
    public Result update(SysTeaching sysTeaching) throws ServiceException {
        try {
            SysTeaching updateTeaching = teachingSystemDao.findById(sysTeaching.getTcstid());
            //根据id查询体系名，并判断体系名是否要修改
            if (!sysTeaching.getTtitle().equals(updateTeaching.getTtitle())){
                SysTeaching teaching = teachingSystemDao.findByTitle(sysTeaching.getTtitle());
                //根据体系名判断体系是否存在
                if (teaching != null){
                    return Result.failed("该体系名已被使用，请重新输入");
                }
            }
            int res = teachingSystemDao.updateById(sysTeaching);
            sysTeaching.setUpdtim(new Date());
            log.info("修改教学体系：{}",sysTeaching);
            return res>0 ? Result.succeed(sysTeaching,"修改成功") : Result.failed("修改失败");
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    /**
     * 查询
     * @param params
     * @return
     */
    @Override
    public PageResult<SysTeaching> findTeaching(Map<String, Object> params) throws ServiceException{
        try {
            int total = teachingSystemDao.count(params);
            List<SysTeaching> teachingList = Collections.emptyList();
            if (total > 0){
                PageUtil.pageParamConver(params, false);
                teachingList = teachingSystemDao.findList(params);
            }
            //收集所有数据
            return PageResult.<SysTeaching>builder().data(teachingList).code(0).count((long) total).build();
        }catch (Exception e){
            throw new ServiceException(e);
        }
    }

    @Override
    public SysTeaching findById(Integer id) throws ServiceException{
        try {
            return teachingSystemDao.findById(id);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }
}
